package com.ruoyi.controller.jpjs.humanresources;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.jpjs.PublicService;
import com.ruoyi.service.jpjs.empchange.EmpChangeService;
import com.ruoyi.service.jpjs.humanresources.HumanResourcesService;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 人力资源成本分析
 * author:zeo
 */

@Controller
@RequestMapping("/humanResources")
public class HumanResourcesController extends BaseController {

    @Autowired
    HumanResourcesService humanResourcesService;

    @Autowired
    EmpChangeService empChangeService;

    @Autowired
    PublicService publicService;

    private String prefix = "jpjs/humanresources";

    @GetMapping()
    public String empChange(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserCompAndJszb());
        return prefix + "/humanResources";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        startPage();
        return getDataTable(humanResourcesService.list(map));
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserCompAndJszb());
        return prefix + "/add";
    }

    @GetMapping("/edit/{id}")
    public String edit(ModelMap mmap, @PathVariable("id") String id) {
        mmap.put("data", humanResourcesService.findDataById(id));
        mmap.put("userComp", publicService.findUserCompAndJszb());
        return prefix + "/edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(humanResourcesService.edit(map));
    }

    @RequestMapping("/costTotal")
    @ResponseBody
    public BaseResult costTotal(@RequestParam Map thisMothsMap) {
        //根据年份和工程公司找出这一年所有的人力支出
        String years = thisMothsMap.get("years").toString();
        String compId = thisMothsMap.get("compId").toString();
        String months = thisMothsMap.get("months").toString();
        //月份数量
        int monthsCumulative = 0;
        //总支出
        int totalExpenditure = 0;
        //总月末人数
        int totalMonthNum = 0;

        for (int i = 1; i <= 12; i++) {
            if(i == Integer.parseInt(months)){
                continue;
            }
            String oid = empChangeService.findOid(years, i + "", compId);
            if(oid != null){
                totalExpenditure += humanResourcesService.getTotalExpenditure(oid);
                totalMonthNum += empChangeService.monthEndNum(oid);
                monthsCumulative++;
            }
        }

        Map map = new HashMap();
        map.put("monthsCumulative",monthsCumulative);
        map.put("totalExpenditure",totalExpenditure);
        map.put("totalMonthNum",totalMonthNum);


        return BaseResult.build(ResultEnum.SUCCESS,map);
    }

}

